if [ "$#" -eq 1 ]
then
  if [ "$1" == "--uninstall" ]
  then
    echo -e "---\e[92mUninstall All\e[0m---"
    sudo rm -rf $HOME/libaacplus-2.0.2
    sudo rm -rf $HOME/faac-1.28
    sudo rm -rf $HOME/ffmpeg
    sudo rm -rf $HOME/libvpx
    sudo rm -rf $HOME/x264
    sudo rm -rf $HOME/x265
    sudo rm /usr/local/lib/libfaac*
    sudo rm /usr/local/lib/libaacplus*
    sudo rm /usr/local/lib/libvpx*
    sudo rm /usr/local/lib/libx26*
    sudo rm /usr/local/lib/libav*
    sudo rm /usr/local/lib/libpost*
    sudo rm /usr/local/lib/libsw*
    sudo rm /usr/local/bin/ffmpeg
    exit 0
  fi
fi
sudo apt-get install build-essential autogen autoconf automake libtool libtheora-dev libvorbis-dev libzvbi-dev
MACHINE=`uname -m`
cpus=`cat /proc/cpuinfo |grep "^processor"|wc -l`
cd $HOME
if [ ! -f "/usr/local/lib/libaacplus.a" ]
then
  echo -e "---\e[92mInstall aacplus\e[0m---"
  if [ ! -d "$HOME/libaacplus-2.0.2" ]
  then
    if [ ! -f "$HOME/libaacplus-2.0.2.tar.gz" ]
    then
      wget http://tipok.org.ua/downloads/media/aacplus/libaacplus/libaacplus-2.0.2.tar.gz 
    fi
    tar xzf libaacplus-2.0.2.tar.gz
  fi

  cd libaacplus-2.0.2
  if [ "$MACHINE" == "armv6l" ] 
  then
    ./autogen.sh --with-parameter-expansion-string-replace-capable-shell=/bin/bash --host=arm-unknown-linux-gnueabi --enable-static
  else
    ./autogen.sh --enable-static --disable-shared
  fi
  make
  sudo make install
fi

cd $HOME
if [ "$MACHINE" == "armv6l" ]
then
  echo "no point of x265 on raspi"
  H265=""
else
  echo -e "---\e[92mInstall x265\e[0m---"
  H265="--enable-libx265 "
  sudo apt-get install mercurial cmake cmake-curses-gui
  if [ ! -f "/usr/local/lib/libx265.a" ]
  then
    echo "install libx265"
    hg clone https://bitbucket.org/multicoreware/x265
    cd x265/build/linux
    cmake -G "Unix Makefiles" ../../source -DBUILD_SHARED_LIBS=OFF -DENABLE_SHARED=OFF
    #./make-Makefiles.bash
    make
    sudo make install
  fi
fi

cd $HOME
if [ "$MACHINE" == "armv6l" ]
then
  if [ ! -f "/usr/local/lib/libfaac.a" ]
  then
    echo -e "---\e[92mInstall libfaac\e[0m---"
    if [ ! -d "$HOME/faac-1.28" ]
    then
      if [ ! -f "$HOME/faac-1.28.tar.gz" ]
      then
        wget http://kent.dl.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
      fi
      tar xzf faac-1.28.tar.gz
    fi

    cd faac-1.28
    patch -N -p1 <~/tudorsuciupersonal/patches/faaccompile.patch
    ./configure --enable-static --disable-shared
    make
    sudo make install
  fi
fi

cd $HOME
if [ ! -f "/usr/local/lib/libvpx.a" ]
then
  echo -e "---\e[92mInstall libvpx\e[0m---"
  if [ ! -d "$HOME/libvpx" ]
  then
    git clone https://chromium.googlesource.com/webm/libvpx
  fi
  cd $HOME/libvpx
  ./configure --enable-static --disable-shared
  make
  sudo make install
fi

cd $HOME
if [ ! -f "/usr/local/lib/libx264.a" ]
then
  echo -e "---\e[92mInstall x264\e[0m---"
  if [ ! -d "$HOME/x264" ]
  then
    git clone git://git.videolan.org/x264.git
  fi
  cd $HOME/x264
  if [ "$MACHINE" == "armv6l" ]
  then
    ./configure --enable-static --disable-shared --disable-lavf --extra-cflags='-march=armv6' --extra-ldflags='-march=armv6'
  else
    ./configure --enable-static --disable-shared --disable-lavf
  fi
  make
  sudo make install
fi

cd $HOME
if [ ! -f "/usr/local/bin/ffmpeg" ]
then
  echo -e "---\e[92mInstall ffmpeg\e[0m---"
  if [ ! -d "$HOME/ffmpeg" ]
  then
    git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
  fi
  cd $HOME/ffmpeg
  make clean
  
  ./configure --enable-libx264 --enable-libvpx \
  --enable-libaacplus --disable-shared --enable-static\
  --enable-libtheora --enable-libvorbis \
  --enable-libzvbi $H265 \
  --enable-gpl --enable-nonfree --enable-libfaac \
  --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib
  make -j $cpus
  sudo make install
fi

